﻿namespace S7.Net.Types
{
    using S7.Net;
    using System;

    public static class Timer
    {
        public static double FromByteArray(byte[] bytes)
        {
            double num = 0.0;
            string str = ((short) Word.FromBytes(bytes[1], bytes[0])).ValToBinString();
            num = str.Substring(4, 4).BinStringToInt32() * 100.0;
            num += str.Substring(8, 4).BinStringToInt32() * 10.0;
            num += str.Substring(12, 4).BinStringToInt32();
            string str2 = str.Substring(2, 2);
            if (str2 == null)
            {
                return num;
            }
            if (!(str2 == "00"))
            {
                if (str2 != "01")
                {
                    if (str2 == "10")
                    {
                        return (num * 1.0);
                    }
                    if (str2 != "11")
                    {
                        return num;
                    }
                    return (num * 10.0);
                }
            }
            else
            {
                return (num * 0.01);
            }
            return (num * 0.1);
        }

        public static double[] ToArray(byte[] bytes)
        {
            double[] numArray = new double[bytes.Length / 2];
            int num = 0;
            for (int i = 0; i < (bytes.Length / 2); i++)
            {
                numArray[i] = FromByteArray(new byte[] { bytes[num++], bytes[num++] });
            }
            return numArray;
        }

        public static byte[] ToByteArray(ushort value)
        {
            byte[] buffer = new byte[2];
            int num = 2;
            long num2 = value;
            for (int i = 0; i < num; i++)
            {
                long num4 = (long) Math.Pow(256.0, (double) i);
                long num5 = num2 / num4;
                buffer[(num - i) - 1] = (byte) (num5 & 0xffL);
                num2 -= buffer[(num - i) - 1] * num4;
            }
            return buffer;
        }

        public static byte[] ToByteArray(ushort[] value)
        {
            ByteArray array = new ByteArray();
            foreach (ushort num in value)
            {
                array.Add(ToByteArray(num));
            }
            return array.array;
        }
    }
}

